package com.gpt.article.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.gpt.article.domain.GptPrompts;
import com.ruoyi.common.core.domain.entity.GptSpiderClass;
import com.gpt.article.domain.GptSpiderItem;
import com.gpt.article.service.IGptSpiderClassService;
import com.ruoyi.common.core.domain.entity.SysDept;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.gpt.article.domain.GptSpiderDiy;
import com.gpt.article.service.IGptSpiderDiyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采集配置Controller
 * 
 * @author zhuzi
 * @date 2023-10-24
 */
@RestController
@RequestMapping("/article/diySpider")
public class GptSpiderDiyController extends BaseController
{
    @Autowired
    private IGptSpiderDiyService gptSpiderDiyService;

    @Autowired
    private IGptSpiderClassService gptSpiderClassService;
    /**
     * 查询采集配置列表
     */
    @PreAuthorize("@ss.hasPermi('article:diySpider:list')")
    @GetMapping("/list")
    public TableDataInfo list(GptSpiderDiy gptSpiderDiy)
    {
        startPage();
        List<GptSpiderDiy> list = gptSpiderDiyService.selectGptSpiderDiyList(gptSpiderDiy);
        return getDataTable(list);
    }


    /**
     * 导出采集配置列表
     */
    @PreAuthorize("@ss.hasPermi('article:diySpider:export')")
    @Log(title = "采集配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GptSpiderDiy gptSpiderDiy)
    {
        List<GptSpiderDiy> list = gptSpiderDiyService.selectGptSpiderDiyList(gptSpiderDiy);
        ExcelUtil<GptSpiderDiy> util = new ExcelUtil<GptSpiderDiy>(GptSpiderDiy.class);
        util.exportExcel(response, list, "采集配置数据");
    }

    /**
     * 获取采集配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('article:diySpider:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(gptSpiderDiyService.selectGptSpiderDiyById(id));
    }

    /**
     * 新增采集配置
     */
    @PreAuthorize("@ss.hasPermi('article:diySpider:add')")
    @Log(title = "采集配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GptSpiderDiy gptSpiderDiy)
    {
        return toAjax(gptSpiderDiyService.insertGptSpiderDiy(gptSpiderDiy));
    }

    /**
     * 修改采集配置
     */
    @PreAuthorize("@ss.hasPermi('article:diySpider:edit')")
    @Log(title = "采集配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GptSpiderDiy gptSpiderDiy)
    {
        return toAjax(gptSpiderDiyService.updateGptSpiderDiy(gptSpiderDiy));
    }

    /**
     * 删除采集配置
     */
    @PreAuthorize("@ss.hasPermi('article:diySpider:remove')")
    @Log(title = "采集配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gptSpiderDiyService.deleteGptSpiderDiyByIds(ids));
    }

    /**
     * 获取采集来源
     */
    @PreAuthorize("@ss.hasPermi('article:diySpider:list')")
    @GetMapping("/getSpiderItem")
    public AjaxResult getSpiderDiy()
    {
//        return success(gptSpiderDiyService.getSpiderItem());
        AjaxResult ajax = AjaxResult.success();
        List<GptSpiderItem> spiderItemList = gptSpiderDiyService.getSpiderItem();
        ajax.put("data", spiderItemList);
        return ajax;

    }


    /**
     * 获取生成模型
     */
    @PreAuthorize("@ss.hasPermi('article:diySpider:list')")
    @GetMapping("/getPromptsItem")
    public AjaxResult getPromptsItem()
    {
        AjaxResult ajax = AjaxResult.success();
        List<GptPrompts> spiderItemList = gptSpiderDiyService.getPrompts();
        ajax.put("data", spiderItemList);
        return ajax;

    }



    /**
     * 获取采集分类树数据
     */
    @PreAuthorize("@ss.hasPermi('article:diySpider:list')")
    @GetMapping("/getSpiderClassTreeData")
    public AjaxResult getSpiderClassTreeData(GptSpiderClass gptSpiderClass)
    {
        return success(gptSpiderClassService.selectSpiderClassTreeList(gptSpiderClass));

    }

    /**
     * 采集
     */
    @Log(title = "执行采集", businessType = BusinessType.SPIDER)
    @GetMapping("/spiderStart/{ids}")
    public AjaxResult spider(@PathVariable  Long[] ids){
        return success(gptSpiderDiyService.spiderArticles(ids));
    }


}
